package cn.edu.ldu.protocol;

import cn.edu.ldu.modules.entity.FriendsEntity;
import cn.edu.ldu.views.modules.controller.UserController;
import com.alibaba.fastjson.JSON;
import cn.edu.ldu.modules.entity.UserEntity;
import cn.edu.ldu.util.Consts;
import cn.edu.ldu.views.modules.controller.ServerViewController;

import java.io.IOException;
import java.util.List;

public class Logout {

    public Logout(Protocol protocol){

        List<FriendsEntity> onlineFriends = JSON.parseArray(protocol.getData(), FriendsEntity.class);

        ServerViewController.getServerView().consoleJTA(onlineFriends.get(0).getUserId() + "下线\n");
        ServerViewController.getServerView().offlineListJTA(String.valueOf(onlineFriends.get(0).getUserId()));

        try {
            UserController.removeUserToList(onlineFriends.get(0).getUserId());

            //通知在线好友 ：“我下线了”
            for (FriendsEntity fe : onlineFriends) {
                protocol.setToken(null);
                protocol.setData(String.valueOf(fe.getUserId()));
                protocol.setKey(Consts.P_LOGOUT);
                UserController.sendDataToUser(fe.getFriendId(), protocol);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
